package com.apps.gomdiem.helper;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.apps.gomdiem.model.MenuList;
import com.apps.gomdiem.model.Store;
import com.apps.gomdiem.model.UserAccount;

public class DataBaseHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private static String DATABASENAME = "GomDiemDB";
	// Table User
	private static String USERS_TABLE = "userTB";
	private static final String KEY_USERS_USERNAME = "username";
	private static final String KEY_USERS_PASSWORD = "password";
	private static final String KEY_USERS_NAME = "name";
	private static final String KEY_USERS_ADDRESS = "address";
	private static final String KEY_USERS_EMAIL = "email";
	private static final String KEY_USERS_PHONE = "phone";
	// Table Store
	private static String STORES_TABLE = "storeTB";
	private static final String KEY_STORE_ID = "idStore";
	private static final String KEY_STORE_NAME = "nameStore";
	private static final String KEY_STORE_ADDRESS = "addressStore";
	private static final String KEY_STORE_IMG = "imgStore";
	private static final String KEY_STORE_POINT = "pointStore";
	// Table Menu
	private static String MENUS_TABLE = "menuTB";
	private static final String KEY_MENU_NAME = "nameMenu";
	private static final String KEY_MENU_PRICE = "priceMenu";
	private static final String KEY_MENU_IMG = "imgMenu";
	private static final String KEY_MENU_POINT = "pointMenu";
	private static final String KEY_MENU_TYPE = "typeMenu";

	Context mContext;

	public DataBaseHelper(Context context) {
		super(context, DATABASENAME, null, DATABASE_VERSION);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_USER_TABLE = "CREATE TABLE " + USERS_TABLE + "("
				+ KEY_USERS_USERNAME + " TEXT," + KEY_USERS_PASSWORD + " TEXT,"
				+ KEY_USERS_NAME + " TEXT," + KEY_USERS_ADDRESS + " TEXT,"
				+ KEY_USERS_EMAIL + " TEXT," + KEY_USERS_PHONE + " TEXT" + ")";
		db.execSQL(CREATE_USER_TABLE);

		String CREATE_STORE_TABLE = "CREATE TABLE " + STORES_TABLE + "("
				+ KEY_STORE_ID + " TEXT," + KEY_STORE_NAME + " TEXT,"
				+ KEY_STORE_ADDRESS + " TEXT," + KEY_STORE_IMG + " TEXT,"
				+ KEY_STORE_POINT + " TEXT" + ")";
		db.execSQL(CREATE_STORE_TABLE);

		String CREATE_MENU_TABLE = "CREATE TABLE " + MENUS_TABLE + "("
				+ KEY_MENU_NAME + " TEXT," + KEY_MENU_PRICE + " INTEGER,"
				+ KEY_MENU_IMG + " TEXT," + KEY_MENU_TYPE + " TEXT,"
				+ KEY_MENU_POINT + " INTEGER" + ")";
		db.execSQL(CREATE_MENU_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS" + USERS_TABLE);
		onCreate(db);

		db.execSQL("DROP TABLE IF EXISTS" + STORES_TABLE);
		onCreate(db);

		db.execSQL("DROP TABLE IF EXISTS" + MENUS_TABLE);
		onCreate(db);
	}

	// insert data into table user
	public void addUserAccount(UserAccount pUserAccount) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("username", pUserAccount.mUserName);
		contentValues.put("password", pUserAccount.mPassword);
		contentValues.put("name", pUserAccount.mName);
		contentValues.put("address", pUserAccount.mAddress);
		contentValues.put("email", pUserAccount.mEmail);
		contentValues.put("phone", pUserAccount.mPhone);
		db.insert(USERS_TABLE, null, contentValues);
		db.close();
	}

	// insert data into table store
	public void addStore(Store pStore) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("idStore", pStore.idStore);
		contentValues.put("nameStore", pStore.nameStore);
		contentValues.put("addressStore", pStore.addressStore);
		contentValues.put("imgStore", pStore.linkImg);
		contentValues.put("pointStore", pStore.point);
		db.insert(STORES_TABLE, null, contentValues);
		db.close();
	}

	// insert data into table menu
	public void addMenu(MenuList pMenu) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("nameMenu", pMenu.nameMenu);
		contentValues.put("priceMenu", pMenu.price);
		contentValues.put("imgMenu", pMenu.linkImg);
		contentValues.put("pointMenu", pMenu.point);
		contentValues.put("typeMenu", pMenu.type);
		db.insert(MENUS_TABLE, null, contentValues);
		db.close();
	}

	public ArrayList<UserAccount> getAllUserAccount() {
		ArrayList<UserAccount> mListUser = new ArrayList<UserAccount>();
		mListUser.clear();

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from userTB", null);
		if (cursor.getCount() != 0) {
			if (cursor.moveToFirst()) {
				do {

					UserAccount userAccount = UserAccount.getInstance(mContext);

					userAccount.mUserName = cursor.getString(cursor
							.getColumnIndex("username"));

					userAccount.mPassword = cursor.getString(cursor
							.getColumnIndex("password"));

					userAccount.mName = cursor.getString(cursor
							.getColumnIndex("name"));

					userAccount.mAddress = cursor.getString(cursor
							.getColumnIndex("address"));

					userAccount.mEmail = cursor.getString(cursor
							.getColumnIndex("email"));

					userAccount.mPhone = cursor.getString(cursor
							.getColumnIndex("phone"));

					mListUser.add(userAccount);

				} while (cursor.moveToNext());
			}
		}
		cursor.close();
		db.close();
		return mListUser;
	}

	// get all account from database
	public UserAccount getUserAccount(String phoneOrMail, String pwd) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query(USERS_TABLE, null, KEY_USERS_EMAIL + "=? OR "
				+ KEY_USERS_PHONE + "=?", new String[] { phoneOrMail,
				phoneOrMail }, null, null, null);
		UserAccount user = null;
		if (cursor != null && cursor.moveToFirst()) {
			user = UserAccount.getInstance(mContext);
			user.mUserName = cursor.getString(cursor.getColumnIndex(KEY_USERS_USERNAME));
			user.mPassword = cursor.getString(cursor.getColumnIndex(KEY_USERS_PASSWORD));
			user.mName = cursor.getString(cursor.getColumnIndex(KEY_USERS_PASSWORD));
			user.mAddress = cursor.getString(cursor.getColumnIndex(KEY_USERS_ADDRESS));
			user.mEmail = cursor.getString(cursor.getColumnIndex(KEY_USERS_EMAIL));
			user.mPhone = cursor.getString(cursor.getColumnIndex(KEY_USERS_PHONE));
			if (!pwd.equals(user.mPassword)) {
				UserAccount.clear();
				user = null;
			}
		}
		cursor.close();
		db.close();
		return user;
	}

	// get all store from database
	public ArrayList<Store> getAllStore() {
		ArrayList<Store> mListStore = new ArrayList<Store>();
		mListStore.clear();

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from storeTB", null);
		if (cursor.getCount() != 0) {
			if (cursor.moveToFirst()) {
				do {

					Store pStore = new Store();

					pStore.idStore = cursor.getString(cursor
							.getColumnIndex("idStore"));

					pStore.nameStore = cursor.getString(cursor
							.getColumnIndex("nameStore"));

					pStore.addressStore = cursor.getString(cursor
							.getColumnIndex("addressStore"));

					pStore.linkImg = cursor.getString(cursor
							.getColumnIndex("imgStore"));

					pStore.point = cursor.getString(cursor
							.getColumnIndex("pointStore"));

					mListStore.add(pStore);

				} while (cursor.moveToNext());
			}
		}
		cursor.close();
		db.close();
		return mListStore;
	}

	// get all menu from database
	public ArrayList<MenuList> getAllMenu(String type) {
		ArrayList<MenuList> mListMenu = new ArrayList<MenuList>();
		mListMenu.clear();

		SQLiteDatabase db = this.getWritableDatabase();
		String query = "Select * FROM menuTB WHERE typeMenu = '" + type + "'";
		Cursor cursor = db.rawQuery(query, null);
		// Cursor cursor =db.rawQuery("SELECT * FROM " + MENUS_TABLE + " WHERE "
		// +
		// KEY_MENU_TYPE + " = " +"'"+ type +"'" + " ORDER BY " + KEY_MENU_NAME
		// + " COLLATE NOCASE", null);
		if (cursor.getCount() != 0) {
			if (cursor.moveToFirst()) {
				do {

					MenuList pMenu = new MenuList();

					pMenu.nameMenu = cursor.getString(cursor
							.getColumnIndex("nameMenu"));

					pMenu.price = cursor.getInt(cursor
							.getColumnIndex("priceMenu"));

					pMenu.linkImg = cursor.getString(cursor
							.getColumnIndex("imgMenu"));

					pMenu.point = cursor.getInt(cursor
							.getColumnIndex("pointMenu"));

					pMenu.type = cursor.getString(cursor
							.getColumnIndex("typeMenu"));

					mListMenu.add(pMenu);

				} while (cursor.moveToNext());
			}
		}
		cursor.close();
		db.close();
		return mListMenu;
	}

	// Update information of user table
	public void updateUserAccount(UserAccount userAccount) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("username", userAccount.mUserName);
		contentValues.put("password", userAccount.mPassword);
		contentValues.put("name", userAccount.mName);
		contentValues.put("address", userAccount.mAddress);
		contentValues.put("email", userAccount.mEmail);
		contentValues.put("phone", userAccount.mPhone);
		db.update("userTB", contentValues, "username = ?",
				new String[] { userAccount.mUserName });

		db.close();
	}

}